package com.such.kit.file.bean;

import java.util.Comparator;
import java.util.Date;

import com.such.kit.validate.ValidateCommons;

public class FileOrder implements Comparator<FileInfo> {

	/** ORDER_MODE_NAME：根据文件名排序 */
	public static final String ORDER_MODE_NAME = "name";
	/** ORDER_MODE_TYPE：根据文件类型排序 */
	public static final String ORDER_MODE_TYPE = "type";
	/** ORDER_MODE_SIZE：根据文件大小排序 */
	public static final String ORDER_MODE_SIZE = "size";
	/** ORDER_MODE_LAST_MODIFIED：根据最后修改时间排序 */
	public static final String ORDER_MODE_LAST_MODIFIED = "lastModified";
	
	private String orderMode = ORDER_MODE_NAME;
	private Boolean isDesc = false;
	private int returnVal1 = 1;
	private int returnVal2 = -1;

	public String getOrderMode() {
		return this.orderMode;
	}

	public void setOrderMode(String orderMode) {
		this.orderMode = orderMode;
	}

	public Boolean getIsDesc() {
		return this.isDesc;
	}

	public void setIsDesc(Boolean isDesc) {
		this.isDesc = isDesc;
		if (isDesc) {
			this.returnVal1 = -1;
			this.returnVal2 = 1;
		} else {
			this.returnVal1 = 1;
			this.returnVal2 = -1;
		}
	}

	public FileOrder() {

	}
	
	public FileOrder(String orderMode, Boolean isDesc) {
		this.orderMode = orderMode;
		this.setIsDesc(isDesc);
	}
	
	public int compare(FileInfo fb1, FileInfo fb2) {
		boolean isDirectory1 = fb1.isDirectory();
		boolean isDirectory2 = fb2.isDirectory();
		if (isDirectory1 && isDirectory2) {
			return order(fb1, fb2);
		} else if (isDirectory1 && !isDirectory2) {
			return -1;
		} else if (!isDirectory1 && isDirectory2) {
			return 1;
		} else {
			return order(fb1, fb2);
		}
	}
	
	private int order(FileInfo fb1, FileInfo fb2) {
		if (ORDER_MODE_NAME.equals(this.orderMode)) {
			String name1 = fb1.getName();
			String name2 = fb2.getName();
			if (name1.compareTo(name2) > 0) {
				return this.returnVal1;
			} else if (name1.compareTo(name2) < 0) {
				return this.returnVal2;
			}
		} else if (ORDER_MODE_TYPE.equals(this.orderMode)) {
			String type1 = fb1.getType();
			String type2 = fb2.getType();
			if (ValidateCommons.isNotEmpty(type1) && ValidateCommons.isNotEmpty(type2)) {
				if (type1.compareTo(type2) > 0) {
					return this.returnVal1;
				} else if (type1.compareTo(type2) < 0) {
					return this.returnVal2;
				}
			} else if (ValidateCommons.isEmpty(type1) && ValidateCommons.isNotEmpty(type2)) {
				return this.returnVal2;
			} else if (ValidateCommons.isNotEmpty(type1) && ValidateCommons.isEmpty(type2)) {
				return this.returnVal1;
			}
		} else if (ORDER_MODE_SIZE.equals(this.orderMode)) {
			Long size1 = fb1.getSize();
			Long size2 = fb2.getSize();
			if (size1 - size2 > 0) {
				return this.returnVal1;
			} else if (size1 - size2 < 0) {
				return this.returnVal2;
			}
		} else if (ORDER_MODE_LAST_MODIFIED.equals(this.orderMode)) {
			Date lastModified1 = fb1.getLastModified();
			Date lastModified2 = fb2.getLastModified();
			if (lastModified1.compareTo(lastModified2) > 0) {
				return this.returnVal1;
			} else if (lastModified1.compareTo(lastModified2) < 0) {
				return this.returnVal2;
			}
		}
		return 0;
	}
	
}
